package si.wasu.checker;

import java.util.ArrayList;
import java.util.List;

import javax.vecmath.Point2f;

import si.wasu.drawable.Triangle;
import si.wasu.drawable.Triangles;
import si.wasu.trik.LevelListener;

public class CouplingResultChecker {

	public CouplingResultChecker() {

	}

	public boolean checkNoTriangleCoupledInThisPosition(float x, float y, List<Triangle> triangles) {
		for (Triangle t : triangles) {
			if (t.getPoint().x == x && t.getPoint().y == y) {
				return false;
			}
		}
		return true;
	}

	public boolean checkAllTrianglesCoupled(List<Triangle> triangles, List<Triangle> gridTriangles) {
		// boolean result = false;

		for (Triangle gt : gridTriangles) {
			Point2f gtPoint = gt.getTriangleCenter();
			if (checkNoTriangleCoupledInThisPosition(gtPoint.x, gtPoint.y, triangles)) {
				return false;
			}
		}
		return true;
		// for (Triangle t : triangles) {
		// if (t.getCoupled()) {
		// result = true;
		// } else {
		// return false;
		// }
		// }
		// return result;
	}

	public boolean getResult(ArrayList<Triangle> triangles, List<Triangle> gridTrianglesUp, List<Triangle> gridTrianglesDown, int triangleWidth) {
		Triangle firstTriangleUp = getTriangleOnGrid(triangles, gridTrianglesUp.get(0));
		String firstgreen = firstTriangleUp.getNumberColor().getGreen();
		String firstblue = firstTriangleUp.getNumberColor().getBlue();
		String firstred = firstTriangleUp.getNumberColor().getRed();

		for (Triangle gt : gridTrianglesDown) {
			Triangle trenutni = getTriangleOnGrid(triangles, gt);

			int rposition = trenutni.getRposition();
			String green = trenutni.getNumberColor().getGreen();
			String blue = trenutni.getNumberColor().getBlue();
			String red = trenutni.getNumberColor().getRed();

			if (green.length() == 0 || red.length() == 0 || blue.length() == 0) {
				return false;
			}
			boolean checkGreen = false;
			boolean checkBlue = false;
			boolean checkRed = false;
			Point2f tmpTrenutni = trenutni.getTriangleCenter();
			switch (rposition) {
			case 1:
				checkBlue = checkEvaluatedRacun(triangles, triangleWidth, trenutni, blue, tmpTrenutni, 0, -triangleWidth, 1);
				checkRed = checkEvaluatedRacun(triangles, triangleWidth, trenutni, red, tmpTrenutni, -triangleWidth / 2, 0, 2);
				checkGreen = checkEvaluatedRacun(triangles, triangleWidth, trenutni, green, tmpTrenutni, triangleWidth / 2, 0, 0);
				if (!checkGreen || !checkBlue || !checkRed) {
					return false;
				}
				break;
			case 3:
				checkGreen = checkEvaluatedRacun(triangles, triangleWidth, trenutni, green, tmpTrenutni, 0, -triangleWidth, 0);
				checkBlue = checkEvaluatedRacun(triangles, triangleWidth, trenutni, blue, tmpTrenutni, -triangleWidth / 2, 0, 1);
				checkRed = checkEvaluatedRacun(triangles, triangleWidth, trenutni, red, tmpTrenutni, triangleWidth / 2, 0, 2);
				if (!checkGreen || !checkBlue || !checkRed) {
					return false;
				}
				break;

			case 5:
				checkRed = checkEvaluatedRacun(triangles, triangleWidth, trenutni, red, tmpTrenutni, 0, -triangleWidth, 2);
				checkGreen = checkEvaluatedRacun(triangles, triangleWidth, trenutni, green, tmpTrenutni, -triangleWidth / 2, 0, 0);
				checkBlue = checkEvaluatedRacun(triangles, triangleWidth, trenutni, blue, tmpTrenutni, triangleWidth / 2, 0, 1);

				if (!checkGreen || !checkBlue || !checkRed) {
					return false;
				}
				break;
			}
		}

		// for (Triangle gt : gridTrianglesUp) {
		// Triangle trenutni = getTriangleOnGrid(triangles, gt);
		// if (trenutni != null) { // metalo null pointer
		// String green = trenutni.getNumberColor().getGreen();
		// String blue = trenutni.getNumberColor().getBlue();
		// String red = trenutni.getNumberColor().getRed();
		// Point2f tmpTrenutni = trenutni.getTriangleCenter();
		//
		// boolean checkGreen = false;
		// boolean checkBlue = false;
		// boolean checkRed = false;
		//
		// if (firstgreen.length() != 0) {
		// checkGreen = checkEvaluatedRacun(triangles, triangleWidth, trenutni,
		// green, tmpTrenutni, 0, triangleWidth, 0);
		// checkBlue = checkEvaluatedRacun(triangles, triangleWidth, trenutni,
		// blue, tmpTrenutni, triangleWidth / 2, 0, 1);
		// checkRed = checkEvaluatedRacun(triangles, triangleWidth, trenutni,
		// red, tmpTrenutni, -triangleWidth / 2, 0, 2);
		// } else if (firstblue.length() != 0) {
		// checkBlue = checkEvaluatedRacun(triangles, triangleWidth, trenutni,
		// blue, tmpTrenutni, 0, triangleWidth, 1);
		// checkRed = checkEvaluatedRacun(triangles, triangleWidth, trenutni,
		// red, tmpTrenutni, triangleWidth / 2, 0, 2);
		// checkGreen = checkEvaluatedRacun(triangles, triangleWidth, trenutni,
		// green, tmpTrenutni, -triangleWidth / 2, 0, 0);
		// } else if (firstred.length() != 0) {
		// checkRed = checkEvaluatedRacun(triangles, triangleWidth, trenutni,
		// red, tmpTrenutni, 0, triangleWidth, 2);
		// checkGreen = checkEvaluatedRacun(triangles, triangleWidth, trenutni,
		// green, tmpTrenutni, triangleWidth / 2, 0, 0);
		// checkBlue = checkEvaluatedRacun(triangles, triangleWidth, trenutni,
		// blue, tmpTrenutni, -triangleWidth / 2, 0, 1);
		// }
		// if (!checkGreen) {
		// return false;
		// }
		// if (!checkBlue) {
		// return false;
		// }
		// if (!checkRed) {
		// return false;
		// }
		// }
		// }

		return true;

	}

	private boolean checkEvaluatedRacun(ArrayList<Triangle> triangles, int triangleWidth, Triangle trenutni, String numberColor, Point2f tmpTrenutni, int offsetX, int offsetY, int color) {
		for (Triangle gledan : triangles) {
			if (numberColor.equals("")) {
				if (gledan.isPointInTriangle(tmpTrenutni.x + offsetX, (float) (tmpTrenutni.y + offsetY))) {
					return false;
				}
			} else if (gledan.isPointInTriangle(tmpTrenutni.x + offsetX, (float) (tmpTrenutni.y + offsetY)) && !numberColor.equals("")) {
				int gledanRPosition = gledan.getRposition();
				int trenutniRPosition = trenutni.getRposition();

				switch (trenutniRPosition) {
				case 1:
					if (gledanRPosition != 4) {
						return false;
					}
					break;
				case 3:
					if (gledanRPosition != 0) {
						return false;
					}
					break;
				case 5:
					if (gledanRPosition != 2) {
						return false;
					}
					break;
				}

				int evaluateGledan = 0;
				int evaluateTrenutni = 0;
				switch (color) {
				case 0:
					evaluateGledan = gledan.getRacun12().getEvaluated();
					evaluateTrenutni = trenutni.getRacun12().getEvaluated();
					if (!(evaluateGledan == evaluateTrenutni)) {
						return false;
					}
					break;
				case 1:
					evaluateGledan = gledan.getRacun23().getEvaluated();
					evaluateTrenutni = trenutni.getRacun23().getEvaluated();
					if (!(evaluateGledan == evaluateTrenutni)) {
						return false;
					}
					break;
				case 2:
					evaluateGledan = gledan.getRacun31().getEvaluated();
					evaluateTrenutni = trenutni.getRacun31().getEvaluated();
					if (!(evaluateGledan == evaluateTrenutni)) {
						return false;
					}
					break;
				}
			}
		}
		return true;
	}

	private Triangle getTriangleOnGrid(ArrayList<Triangle> triangles, Triangle gt) {
		for (Triangle t : triangles) {
			Point2f tmp = gt.getTriangleCenter();
			if (t.getPoint().x == tmp.x && t.getPoint().y == tmp.y) {
				return t;
			}
		}
		return null;
	}

	public int getResultPoints(long timePast, ArrayList<Triangle> size) {
		int konst=500000;
		float sizeValue=0.3f;
		float timeValue=0.7f;
		int lvl=Integer.parseInt(LevelListener.level.split(" ")[1]);
		return (int) (konst*sizeValue*size.size()/(timeValue*timePast)*lvl*1.2f);
	}
}
